package com.cpr.demo.controller;

import com.cpr.demo.config.ThreadPoolConfig;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.Executor;

/**
 * @author Chen Panrui
 * created at 2021/9/7 14:45
 */
@RestController
@RequestMapping("/log")
@Slf4j
public class LogTestController {

    @Autowired
    private Executor executorService;

    /**
     * 包装处理MDC 的 线程池
     * {@link ThreadPoolConfig#configMdcThreadPool()}
     */
    @Autowired
    @Qualifier("executorWrappedMDC")
    private Executor executorWrappedMdc;

    @GetMapping("/log1")
    public String log1() throws InterruptedException {
        log.info("主线程打印hello");
        executorService.execute(()-> log.info("子线程打印hello-打印不了requestId"));
        Thread.sleep(100);
        executorWrappedMdc.execute(new Runnable() {
            @Override
            public void run() {
                log.info("子线程再次打印hello-可以打印requestId");
            }
        });
        return "success";
    }

}
